package com.ruoyi.web.controller.assets;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.OliveAssets;
import com.ruoyi.system.domain.OliveAssetsRepair;
import com.ruoyi.system.domain.ScrapAudit;
import com.ruoyi.system.service.IScrapAuditService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OliveAssetsScrap;
import com.ruoyi.system.service.IOliveAssetsScrapService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资产报废Controller
 * 
 * @author hzl
 * @date 2025-03-24
 */
@RestController
@RequestMapping("/assets/scrap")
public class OliveAssetsScrapController extends BaseController
{
    @Autowired
    private IOliveAssetsScrapService oliveAssetsScrapService;
    @Autowired
    private IScrapAuditService scrapAuditService;

    /**
     * 查询资产报废列表
     */
    @PreAuthorize("@ss.hasPermi('assets:scrap:list')")
    @GetMapping("/list")
    public TableDataInfo list(OliveAssetsScrap oliveAssetsScrap)
    {
        startPage();
        List<OliveAssetsScrap> list = oliveAssetsScrapService.selectOliveAssetsScrapList(oliveAssetsScrap);
        return getDataTable(list);
    }

    /**
     * 导出资产报废列表
     */
    @PreAuthorize("@ss.hasPermi('assets:scrap:export')")
    @Log(title = "资产报废", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OliveAssetsScrap oliveAssetsScrap)
    {
        List<OliveAssetsScrap> list = oliveAssetsScrapService.selectOliveAssetsScrapList(oliveAssetsScrap);
        ExcelUtil<OliveAssetsScrap> util = new ExcelUtil<OliveAssetsScrap>(OliveAssetsScrap.class);
        util.exportExcel(response, list, "资产报废数据");
    }

    /**
     * 获取资产报废详细信息
     */
    @PreAuthorize("@ss.hasPermi('assets:scrap:query')")
    @GetMapping(value = "/{assetId}")
    public AjaxResult getInfo(@PathVariable("assetId") String assetId)
    {
        return success(oliveAssetsScrapService.selectOliveAssetsScrapByAssetId(assetId));
    }

    /**
     * 新增资产报废
     */
    @PreAuthorize("@ss.hasPermi('assets:scrap:add')")
    @Log(title = "资产报废", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OliveAssetsScrap oliveAssetsScrap)
    {
        return toAjax(oliveAssetsScrapService.insertOliveAssetsScrap(oliveAssetsScrap));
    }

    /**
     * 修改资产报废
     */
    @PreAuthorize("@ss.hasPermi('assets:scrap:edit')")
    @Log(title = "资产报废", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OliveAssetsScrap oliveAssetsScrap)
    {
        return toAjax(oliveAssetsScrapService.updateOliveAssetsScrap(oliveAssetsScrap));
    }

    /**
     * 删除资产报废
     */
    @PreAuthorize("@ss.hasPermi('assets:scrap:remove')")
    @Log(title = "资产报废", businessType = BusinessType.DELETE)
	@DeleteMapping("/{assetIds}")
    public AjaxResult remove(@PathVariable String[] assetIds)
    {
        return toAjax(oliveAssetsScrapService.deleteOliveAssetsScrapByAssetIds(assetIds));
    }

    /**
     * 提交报废申请
     */
    @PreAuthorize("@ss.hasPermi('assets:scrap:request')")
    @Log(title = "资产报废申请", businessType = BusinessType.INSERT)
    @PostMapping("/request")
    public AjaxResult requestScrap(
            @RequestBody OliveAssetsScrap oliveAssetsScrap,
            @RequestParam("scrapReason") String scrapReason // 新增参数
    ) {
        Long userId = Long.valueOf(getUserId());
        String assetId = oliveAssetsScrap.getAssetId();

        ScrapAudit scrapAudit = new ScrapAudit();
        scrapAudit.setUserId(userId);
        scrapAudit.setAssetId(assetId);
        scrapAudit.setScrapReason(scrapReason); // 使用新增参数
        scrapAudit.setStatus(0L);
        // 设置申请时间
        scrapAudit.setApplyTime(new Date());
        oliveAssetsScrap.setAssetStatus(5L);
        oliveAssetsScrapService.updateOliveAssetsScrap(oliveAssetsScrap);
        return toAjax(scrapAuditService.insertScrapAudit(scrapAudit));
    }
}
